package org.iteam.xlz.core.service.membershipcard;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.iteam.commons.utils.DateUtil;
import org.iteam.core.utils.SqlBuilder;
import org.iteam.db.BaseService;
import org.iteam.xlz.core.entity.membershipcard.MembershipCardType;

public class CardTypeService extends BaseService<MembershipCardType>{
	static CardTypeService instance=null;
	
	private CardTypeService(){}
	
	public static CardTypeService getInstance(){
		if(null==instance){
			instance=new CardTypeService();
		}
		return instance;
	}
	
	public long counts(String typeparent){
		try {
			StringBuilder sql=new StringBuilder("select count(1) from membershipcardtype where 1=1");
			List<Object> params=new ArrayList<Object>();
			if(StringUtils.isNotBlank(typeparent)){
				sql.append(" and typeparent=?");
				params.add(typeparent);
			}else{
				sql.append(" and typeparent is null");
			}
			return super.counts(sql.toString(),params.toArray());
		} catch (SQLException e) {
			e.printStackTrace();
			return 0;
		}
	}
	
	public List<MembershipCardType> query(String typeparent,long page,long rows){
		try {
			StringBuilder sql=new StringBuilder("select * from membershipcardtype where 1=1");
			List<Object> params=new ArrayList<Object>();
			if(StringUtils.isNotBlank(typeparent)){
				sql.append(" and typeparent=?");
				params.add(typeparent);
			}else{
				sql.append(" and typeparent is null");
			}
			sql.append(" order by createtime limit ?,?");
			params.add((page-1)*rows);
			params.add(rows);
			return super.query(sql.toString(),params.toArray());
		} catch (SQLException e) {
			e.printStackTrace();
			return new ArrayList<MembershipCardType>();
		}
	}
	
	public boolean create(MembershipCardType model){
		model.setCreatetime(DateUtil.currentTime());
		model.setMtid(UUID.randomUUID().toString().replace("-","").toUpperCase());
		
		SqlBuilder sql=SqlBuilder.createInsert("membershipcardtype");
		sql.addField("mtid",model.getMtid());
		if(StringUtils.isNotBlank(model.getTypeparent())){
			sql.addField("typeparent",model.getTypeparent());
		}
		if(StringUtils.isNotBlank(model.getDiscount())){
			sql.addField("discount",model.getDiscount());
		}
		sql.addField("typename",model.getTypename());
		sql.addField("status",model.getStatus());
		sql.addField("createtime",model.getCreatetime());
		sql.addField("creator",model.getCreator());
		try {
			return super.updates(sql.getSql(),sql.getParams())>0;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public MembershipCardType findOne(String mtid){
		try {
			return super.show("select * from membershipcardtype where 1=1 and mtid=?",mtid);
		} catch (SQLException e) {
			e.printStackTrace();
			return new MembershipCardType();
		}
	}
	
	public boolean update(MembershipCardType model){
		SqlBuilder sql=SqlBuilder.createInsert("membershipcardtype");
		if(StringUtils.isNotBlank(model.getDiscount())){
			sql.addField("discount",model.getDiscount());
		}
		sql.addField("typename",model.getTypename());
		sql.addField("status",model.getStatus());
		sql.addWhere("mtid",model.getMtid());
		try {
			return super.updates(sql.getSql(),sql.getParams())>0;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean delete(String mtid){
		try {
			return super.updates("delete from membershipcardtype where 1=1 and mtid=?",mtid)>0;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public List<MembershipCardType> getAll(){
		StringBuilder sql=new StringBuilder("select * from membershipcardtype where 1=1");
		try {
			return super.query(sql.toString());
		} catch (SQLException e) {
			e.printStackTrace();
			return new ArrayList<MembershipCardType>();
		}
	}
	
	private List<MembershipCardType> leafList=null;
	private List<TypeTree> typetree=null;
	public void equipProductType(){
		List<MembershipCardType> list=CardTypeService.getInstance().getAll();
		List<MembershipCardType> topList=new ArrayList<MembershipCardType>();
		leafList=new ArrayList<MembershipCardType>();
		for(MembershipCardType p:list){
			if(p.getStatus()<1){
				if(StringUtils.isNotBlank(p.getTypeparent())){
					leafList.add(p);
				}else{
					topList.add(p);
				}
			}
		}
		typetree=new ArrayList<TypeTree>();
		for(MembershipCardType p:topList){
			TypeTree tt=new TypeTree();
			tt.setId(p.getMtid());
			tt.setText(p.getTypename());
			List<TypeTree> children=new ArrayList<TypeTree>();
			for(MembershipCardType pt:leafList){
				if(pt.getTypeparent().equals(p.getMtid())){
					TypeTree tte=new TypeTree();
					tte.setId(pt.getMtid());
					tte.setText(pt.getTypename());
					children.add(tte);
				}
			}
			tt.setChildren(children);
			typetree.add(tt);
		}
	}
	
	public String getTypeParent(String ptid){
		String s="";
		for(MembershipCardType p:leafList){
			if(p.getMtid().equals(ptid)){
				s=p.getTypeparent();
			}
		}
		return s;
	}
	
	public List<TypeTree> getTypeTree(){
		equipProductType();
		return typetree;
	}
	public class TypeTree{
		String id;
		String text;
		List<TypeTree> children;
		public String getId() {
			return id;
		}
		public void setId(String id) {
			this.id = id;
		}
		public String getText() {
			return text;
		}
		public void setText(String text) {
			this.text = text;
		}
		public List<TypeTree> getChildren() {
			return children;
		}
		public void setChildren(List<TypeTree> children) {
			this.children = children;
		}
		
	}
}
